<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="监控名称" prop="cameraName">
        <el-input v-model="queryParams.cameraName" placeholder="请输入监控名称" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="监控ID" align="center" prop="cameraId" width="100"/>
      <el-table-column label="监控名称" align="center" prop="cameraName" :show-overflow-tooltip="true"/>

      <el-table-column label="监控类型" align="center" prop="status">
        <template v-slot="scope">
          {{ cameraArr[scope.row.cameraType] }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
<!--          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)"-->
<!--          >查看-->
<!--          </el-button>-->
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>


    <!-- 添加或修改监控对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="监控名称" prop="cameraName">
              <el-input v-model="form.cameraName" placeholder="请输入监控名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="监控类型" prop="cameraType">
              <el-select v-model="form.cameraType" placeholder="请选择监控类型" style="width: 100%">
                <el-option
                  v-for="dict in cameraTypes"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="监控地址" prop="cameraAddr">
              <el-input v-model="form.cameraAddr" type="textarea" placeholder="请输入监控地址"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--    查看监控-->
    <el-dialog title="查看监控" :visible.sync="openCamera" width="780px" append-to-body>
      <img
        v-if="openCamera"
        :src="cameraSrc"
        class="popup-img"
      />
    </el-dialog>
  </div>
</template>

<script>
import {cameraAdd, cameraDel, cameraList, cameraEdit} from "@/api/camera/camera";
import Editor from '@/components/Editor';


export default {
  components: {
    Editor
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 监控表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        cameraName: '',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cameraName: [
          {required: true, message: "监控名称不能为空", trigger: "blur"}
        ],
        cameraType: [
          {required: true, message: "监控类型不能为空", trigger: "change"}
        ],
        cameraAddr: [
          {required: true, message: "监控地址不能为空", trigger: "change"}
        ]
      },

      cameraArr: ["关闭", "AI识别", "人脸识别", "监控"], //0关闭、1AI识别、2人脸识别、3监控
      cameraTypes: [{value: '0', label: '关闭'}, {value: '1', label: 'AI识别'}, {value: '2', label: '人脸识别'}, {
        value: '3',
        label: '监控'
      }],
      openCamera: false,
      cameraSrc: ''

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询监控列表 */
    getList() {
      this.loading = true;
      cameraList(this.queryParams).then(response => {
        this.dataList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cameraId: '',
        cameraName: '',
        cameraAddr: '',
        cameraType: '',
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加监控";
    },
    /*查看操作*/
    handleView(row) {
      this.cameraSrc = process.env.VUE_APP_BASE_API + '/api/monitor/player?cameraId=' + row.cameraId;
      this.openCamera = true
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = JSON.parse(JSON.stringify(row))
      this.open = true;
      this.title = "修改监控";
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cameraId) {
            cameraEdit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            cameraAdd(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.cameraId || this.ids
      this.$modal.confirm('是否确认删除监控ID为"' + ids + '"的数据项?').then(function () {
        return cameraDel(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
  }
};
</script>

<style>
.popup-img {
  width: 740px;
  height: 300px;
}
</style>
